// pages/addCard/addCard.js
import WxValidate from '../../utils/WxValidate.js';
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isDefault:0,
    r:0,//1是从佣金提现过来2是管理过来
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({r:options.r})
    this.initValidate(); 
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  switchChange: function (e) {//设为默认
    if (e.detail.value) {
      this.setData({ isDefault: 1 })
    } else {
      this.setData({ isDefault: 0 })
    }
  },
  //表单验证
  initValidate() {
    const rules = {
      bankname: {
        required: true
      },
      bankCard: {
        required: true
      },     
      cardholder: {
        required: true
      }
    }
    const messages = {
      bankCard: {
        required: '请输入银行卡号码'

      },
      bankname: {
        required: '请输入开户行'

      },
      cardholder: {
        required: "请输入真实姓名"
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  showModal: function (error) {
    wx.showToast({
      title:error.msg,
      icon:'none'
    })
  },
  subBind: function (e) {//绑定
    let parames = e.detail.value
    let self=this;
    if (!this.WxValidate.checkForm(parames)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false;
    }
    wx.request({
      url: app.globalData.reqPath + 'shop/MembersBanks',
      header: {
        'authorization': app.globalData.authorization
      },
      method: 'POST',
      data: {
        bankCard: parames.bankCard,
        cardholder: parames.cardholder,
        isDefault: this.data.isDefault,
        openingBank:parames.bankname
      },
      success: function ({ data }) {
        if (data.code == 200) {
          wx.showToast({
            title: '添加成功',
            icon: 'none',
            success: function () {
             if(self.data.r==1){
              wx.redirectTo({
                url: '/pages/cardlist/cardlist',
              })
             }else{
               wx.redirectTo({
                 url: '/pages/cardmanage/cardmanage',
               })
             } 
            }
          })

        } else {
          wx.showToast({
            title: data.data.msg,
            icon: 'none'
          })
        }
      }
    })
  }
})